package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/4/16 11:02
 * https://leetcode-cn.com/problems/minesweeper/
 */
public class Title529 {

    int n, m;

    public char[][] updateBoard(char[][] board, int[] click) {
        n = board.length;
        m = board[0].length;
        int[][] visited = new int[n][m];
        dfs(board, visited, click[0], click[1]);
        return board;
    }

    private void dfs(char[][] board, int[][] visited, int i, int j) {
        //超范围、访问过、1<=board[i]<=8 都直接返回
        if (i < 0 || i >= n || j < 0 || j >= m || visited[i][j] == 1) {
            return;
        }
        visited[i][j] = 1;
        if(board[i][j] >= '1' && board[i][j] <= '8'){
            return;
        }
        //有地雷，标注
        if(board[i][j] == 'M'){
            board[i][j] = 'X';
            return;
        }
        //空格挖出
        if (board[i][j] == 'E') {
            //判断相邻
            //标志，判断四周是否有地雷
            int count = 0;
            int xStart = i - 1 < 0 ? i : i - 1, xEnd = i + 1 >= n ? i : i + 1;
            int yStart = j - 1 < 0 ? j : j - 1, yEnd = j + 1 >= m ? j : j + 1;
            for (int k = xStart; k <= xEnd; k++) {
                for (int l = yStart; l <= yEnd; l++) {
                    //是地雷
                    if (board[k][l] == 'M') {
                        count++;
                    }
                }
            }
            if (count == 0) {
                board[i][j] = 'B';
                //递归（周围8个）
                dfs(board, visited, i - 1, j - 1);
                dfs(board, visited, i - 1, j );
                dfs(board, visited, i - 1, j + 1);
                dfs(board, visited, i, j - 1);
                dfs(board, visited, i , j );
                dfs(board, visited, i, j + 1);
                dfs(board, visited, i +1, j - 1);
                dfs(board, visited, i + 1, j);
                dfs(board, visited, i + 1, j + 1);
            }else{
                board[i][j] = (char) (count + 48);
            }
        }
    }

    public static void main(String[] args) {
        int a = 4;
        char c = (char) (a + 48);
        System.out.println(c);
    }
}
